/*
 * Copyright (C) 2011 UBIVELOX Co., Ltd. #1301 Kolong Digital Villant 222-7,
 * Guro-Dong Guro-Gu, Seoul, KOREA All rights reserved.
 * 
 * http://www.ubivelox.com
 * 
 * This software is the confidential and proprietary information of UBIVELOX
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with UBIVELOX.
 */

package com.wefans.wefansapp.common.tool.utils;


import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;


public class MFEncryptionUtil
{
    private static final String ENCRYPTION_KEY = "";
    private static final String ALGORITHM = "AES/CBC/PKCS7Padding";


    public static SecretKeySpec getKeySpec(String keyStr) throws IOException,
            NoSuchAlgorithmException
    {
        return new SecretKeySpec(keyStr.getBytes(), ALGORITHM);
    }


    public static String encrypt(String input)
    {
        SecretKeySpec spec;

        if (input == null) {
            return null;
        }

        if (input.length() == 0) {
            return input;
        }

        try {
            spec = getKeySpec(ENCRYPTION_KEY);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, spec);
            byte[] encoded = Base64.encode(cipher.doFinal(input.getBytes()), Base64.NO_WRAP);
            return new String(encoded);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return null;
    }


    public static String decrypt(String input)
    {
        SecretKeySpec spec;

        if (input == null) {
            return null;
        }

        if (input.length() == 0) {
            return input;
        }

        try {
            spec = getKeySpec(ENCRYPTION_KEY);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, spec);
            return new String(cipher.doFinal(Base64.decode(input, Base64.NO_WRAP)));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return null;
    }

}
